/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */
package org.gokeep.common.component;

import jakarta.annotation.Resource;
import org.gokeep.common.enums.RunEnvironmentEnum;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class EnvComponent {
    @Resource
    private Environment environment;

    /**
     * 判断激活的配置文件是否有生产环境
     * @return
     */
    public boolean hasProdEnv() {
        return isEnv(RunEnvironmentEnum.PROD);
    }

    /**
     * 判断激活的配置文件是否有开发环境
     * @return
     */
    public boolean hasDevEnv() {
        return isEnv(RunEnvironmentEnum.DEV);
    }


    /**
     * 校验环境配置，必须包含system配置
     * @return
     */
    public boolean verifyEnvActive() {
        return isEnv(RunEnvironmentEnum.SYSTEM);
    }

    public boolean isEnv(RunEnvironmentEnum environment) {
        for (String env: getActiveEnvs()) {
            if (env != null && env.equalsIgnoreCase(environment.getCode())) {
                return true;
            }
        }
        return false;
    }

    private String[] getActiveEnvs() {
        return environment.getActiveProfiles();
    }
}
